package com.cskaoyan.mall.controller.mall;

import com.cskaoyan.mall.model.Result;
import com.cskaoyan.mall.model.User;
import com.cskaoyan.mall.model.bo.AskGoodsMsgBO;
import com.cskaoyan.mall.model.vo.*;
import com.cskaoyan.mall.service.GoodsService;
import com.cskaoyan.mall.service.GoodsServiceImpl;
import com.cskaoyan.mall.service.UserService;
import com.cskaoyan.mall.service.UserServiceImpl;
import com.cskaoyan.mall.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * User: zsquirrel
 * Date: 2020/5/12
 * Time: 6:36 下午
 */
@WebServlet("/api/mall/goods/*")
public class GoodsServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsServiceImpl();

    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/goods/", "");
        if("askGoodsMsg".equals(action)){
            askGoodsMsg(request, response);
        }
    }


    private void askGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AskGoodsMsgBO askGoodsMsgBO = gson.fromJson(requestBody, AskGoodsMsgBO.class);
        goodsService.askGoodsMsg(askGoodsMsgBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        GoodsInfoVO goodsInfo = goodsService.goodsInfo(id);
        response.getWriter().println(gson.toJson(Result.ok(goodsInfo)));
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/goods/", "");
        if("getGoodsInfo".equals(action)){
            getGoodsInfo(request, response);
        }else if("getGoodsMsg".equals(action)){
            getGoodsMsg(request, response);
        }else if("getGoodsComment".equals(action)){
            getGoodsComment(request, response);
        }
    }

    private void getGoodsComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String goodsId = request.getParameter("goodsId");
        GoodsCommentListVO commentListVO = goodsService.getGoodsComment(goodsId);
        response.getWriter().println(gson.toJson(Result.ok(commentListVO)));
    }

    private void getGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        List<GoodsInfoMsgVO> allMsgs = goodsService.getAllMsgs(id);
        response.getWriter().println(gson.toJson(Result.ok(allMsgs)));
    }

}
